package com.turing.post.log.util;

import java.io.File;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.naming.InitialContext;

import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.RollingFileAppender;

import weblogic.logging.WLLogRecord;

public class PCPSLogOut {
	private Logger logger = null;
	public static String getWeblogicDomainDirectory() {
		String wlsDomainDir = System.getProperty("weblogic.domainDir");
		if (wlsDomainDir == null || "".equals(wlsDomainDir)) {
			try {
				InitialContext ctx = new InitialContext();
				MBeanServer server = (MBeanServer) ctx
						.lookup("java:comp/env/jmx/runtime");
				ObjectName service = new ObjectName(
						"com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
				ObjectName domain = (ObjectName) server.getAttribute(service,
						"DomainConfiguration");
				wlsDomainDir = (String) server.getAttribute(domain,
						"RootDirectory");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return wlsDomainDir;
	}

	public static String getWeblogicServerName() {
		String wlsServerName = System.getProperty("weblogic.Name");
		return wlsServerName;
	}

	public PCPSLogOut(String logfullpath) {
		logger = Logger.getLogger(logfullpath);
		// 应用滚动日志
		RollingFileAppender rfa = new RollingFileAppender();
		rfa.setMaxFileSize("4MB");
		rfa.setMaxBackupIndex(128);
		String pathsep = File.separator;
		String[] fn = logfullpath.split("/");
		StringBuilder applogs = new StringBuilder();
		applogs.append(getWeblogicDomainDirectory());
		applogs.append(pathsep);
		applogs.append(getWeblogicServerName());
		applogs.append(pathsep);
		applogs.append("logs");

		// 设置读写权限
		File pfile = new File(applogs.toString());
		// SecurityManager sm = new SecurityManager();
		// sm.checkWrite(applogs.toString());
		pfile.setWritable(true);
		pfile.setReadable(true);

		applogs = new StringBuilder();
		applogs.append(System.getProperty("pcpslog.path"));
		// 确保目录存在
		for (int i = 0; i < fn.length; i++) {
			applogs.append(pathsep);
			applogs.append(fn[i]);
		}

		// File pathfile = new File(applogs.toString());
		// System.out.println("APPLOGPATH: " + applogs.toString() + " return : "
		// + pathfile.mkdirs());

		// 真正的全路径LOG文件名
		applogs.append(pathsep);
		applogs.append(fn[fn.length - 1]);
		applogs.append(".log");

		logger.setAdditivity(true);

		PatternLayout layout = new PatternLayout();
		// log的输出形式
		String conversionPattern = "[%d] - %m%n";
		layout.setConversionPattern(conversionPattern);
		rfa.setLayout(layout);
		// log输出路径
		// System.out.println("APPLOGFILE: " + applogs.toString());
		rfa.setFile(applogs.toString());
		// log的文字码
		// rfa.setEncoding("gb18030");
		// true:在已存在log文件后面追加 false:新log覆盖以前的log
		rfa.setAppend(true);
		// 适用当前配置
		rfa.activateOptions();
		logger.setLevel(org.apache.log4j.Level.ALL);
		logger.addAppender(rfa);
	}

	public void log(WLLogRecord wlLogRecord) {
		// Date now = new Date(wlLogRecord.getTimestamp());
		// String ts = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(now);
		//logger.getAppender("").
		if (wlLogRecord.getThrown() != null)
			logger.info(wlLogRecord.getMessage(), wlLogRecord.getThrown());
		else
			logger.info(wlLogRecord.getMessage());
	}
}
